package com.lhn.bbs.utils;

import com.huaban.analysis.jieba.JiebaSegmenter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class JiebaUtils {

    private JiebaSegmenter segmenter = new JiebaSegmenter();

    public String higeword(String s){
        List<String> fenci = fenci(s);
        Map<String, Integer> tiqugaopinci = tiqugaopinci(fenci);
        List<String> gaopincixianshi = gaopincixianshi(tiqugaopinci);
        StringBuffer stringBuffer = new StringBuffer();
        if(gaopincixianshi != null){
            for(String word : gaopincixianshi){

                stringBuffer.append(word+",");
            }
        }
        String s1 = stringBuffer.toString();
        return s1 ;

    }

    //  将字符进行分词处理
    public List<String> fenci (String replycentent){

        List<String> strings = segmenter.sentenceProcess(replycentent);
        return  strings ;
    }

    //  将分割好的词组进行词频统计,统计出每个词组出现的次数
    public Map<String,Integer> tiqugaopinci (List<String> strings){


        Map<String,Integer> repeatmap = new HashMap<>();

        // 从数组中将词组取出,并找出相同词组出现的次数
        if(strings.size()>0) {
            for (String parent : strings) {
                Integer count = 1;
                if (repeatmap.get(parent)!=null) {
                    count = repeatmap.get(parent) + 1;
                }
                repeatmap.put(parent,count);
            }
        }
        return repeatmap ;
    }

    //将map集合中value>5的key值取出
    public List<String> gaopincixianshi(Map<String,Integer> map){

        List<String> higeword = new ArrayList<>();
        for(String key : map.keySet()){
            Integer count = map.get(key);
            if(count >3 && key != ",," && key !=",。" && key != "，，" && !key.equals("的") && !key.equals("你")
                    && !key.equals("我") && !key.equals("哦")){
                higeword.add(key);
            }
//            System.out.println("Key = " + key + ", Value = " + o);
        }

        return higeword;
    }

}
